﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace brjostagjof.Models
{
    public class DataRepository : IDataRepository
    {
        BrjostagjofDataContext db = new BrjostagjofDataContext();

        #region FrontPage
        public IEnumerable<ArticleBase> FrontPageLatestTen()
        {
            IEnumerable<ArticleBase> result = (from a in db.ArticleBases
                                               where a.displayed == true
                                               orderby a.InformationBase.date descending
                                               select a).Take(10);
            return result;
        }

        public IEnumerable<New> NewsLatestFive()
        {
            IEnumerable<New> result = (from n in db.News
                                       where n.ArticleBase.displayed == true
                                       && n.ArticleBase.ArticleCategory.type == "News"
                                       orderby n.ArticleBase.InformationBase.date descending
                                       select n).Take(5);
            return result;
        }

        public IEnumerable<Article> ArticlesLatestFive()
        {
            IEnumerable<Article> result = (from a in db.Articles
                                           where a.ArticleBase.displayed == true
                                           && a.ArticleBase.ArticleCategory.type == "Articles"
                                           orderby a.ArticleBase.InformationBase.date descending
                                           select a).Take(5);
            return result;
        }

        public IEnumerable<Question> QuestionsLatestFive()
        {
            IEnumerable<Question> result = (from q in db.Questions
                                            where q.ArticleBase.displayed
                                            && q.ArticleBase.ArticleCategory.type == "Questions"
                                            orderby q.ArticleBase.InformationBase.date descending
                                            select q).Take(5);
            return result;
        }

        #endregion

        #region News

        public void AddNews(NewsContainer news)
        {
            db.InformationBases.InsertOnSubmit(news.infoBase);
            db.SubmitChanges();
            int ID = news.infoBase.ID;
            news.artBase.informationBaseID = ID;
            news.news.articleBaseID = ID;
            db.ArticleBases.InsertOnSubmit(news.artBase);
            db.News.InsertOnSubmit(news.news);
            db.SubmitChanges();
        }

        public void UpdateNews(New news)
        {
            New result = GetNewsByID(news.articleBaseID);
            if (result != null)
            {
                result.ArticleBase.displayed = news.ArticleBase.displayed;
                result.ArticleBase.articleCategoryID = news.ArticleBase.ArticleCategory.ID;
                result.ArticleBase.InformationBase.context = news.ArticleBase.InformationBase.context;
                result.ArticleBase.InformationBase.headline = news.ArticleBase.InformationBase.headline;
            }
            db.SubmitChanges();
        }

        public void DeleteNews(New news)
        {
            db.News.DeleteOnSubmit(news);
            db.SubmitChanges();
        }

        public IEnumerable<New> GetHiddenNews()
        {
            IEnumerable<New> result = from n in db.News
                                      where n.ArticleBase.displayed != true
                                      select n;
            return result;
        }

        public New GetNewsByID(int ID)
        {
            New result = (from n in db.News
                          where n.articleBaseID == ID
                          select n).SingleOrDefault();
            return result;
        }

        public IEnumerable<New> GetNewsByCategory(ArticleCategory category)
        {
            IEnumerable<New> result = from n in db.News
                                      where n.ArticleBase.articleCategoryID == category.ID
                                      && n.ArticleBase.displayed == true
                                      && n.ArticleBase.ArticleCategory.type == "News"
                                      orderby n.ArticleBase.InformationBase.date descending
                                      select n;
            return result;
        }

        public List<SelectListItem> GetNewsList()
        {
            var articleCategories = GetNewsCategories();
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var arty in articleCategories)
            {
                items.Add(new SelectListItem { Text = arty.name, Value = arty.ID.ToString() });
            }
            return items;
        }

        public IEnumerable<New> GetAllNews()
        {
            IEnumerable<New> result = (from k in db.News
                                       where k.ArticleBase.displayed == true
                                       && k.ArticleBase.ArticleCategory.type == "News"
                                       orderby k.ArticleBase.InformationBase.date descending
                                       select k);
            return result;
        }

        public IEnumerable<New> GetNewsByCategoryID(int ID)
        {
            IEnumerable<New> result = (from b in db.News
                                           where b.ArticleBase.displayed == true
                                           && b.ArticleBase.articleCategoryID == ID
                                           && b.ArticleBase.ArticleCategory.type == "News"
                                           orderby b.ArticleBase.InformationBase.date descending
                                           select b);
            return result;
        }

        public IEnumerable<ArticleCategory> GetNewsCategories()
        {
            IEnumerable<ArticleCategory> result = (from ac in db.ArticleCategories
                                                   where ac.type == "News"
                                                   select ac);
            return result;
        }

        public IEnumerable<New> GetNewsByCategoryIDHeadlineAlphabetical(int ID)
        {
            IEnumerable<New> result = (from b in db.News
                                       where b.ArticleBase.displayed == true
                                       && b.ArticleBase.articleCategoryID == ID
                                       && b.ArticleBase.ArticleCategory.type == "News"
                                       orderby b.ArticleBase.InformationBase.headline ascending
                                       select b);
            return result;
        }

        public IEnumerable<New> Get10NewsByPageID(int ID)
        {
            IEnumerable<New> result = (from f in db.News
                                           where f.ArticleBase.displayed == true
                                           && f.ArticleBase.ArticleCategory.type == "News"
                                           orderby f.ArticleBase.InformationBase.date descending
                                           select f).Skip((ID) * 10).Take(10);
            return result;
        }

        public IEnumerable<New> NewsLatestTen()
        {
            IEnumerable<New> result = (from n in db.News
                                       where n.ArticleBase.displayed == true
                                       && n.ArticleBase.ArticleCategory.type == "News"
                                       orderby n.ArticleBase.InformationBase.date descending
                                       select n).Take(10);
            return result;
        }

        #endregion

        #region Article

        public void AddArticle(ArticleContainer article)
        {
            db.InformationBases.InsertOnSubmit(article.infoBase);
            db.SubmitChanges();
            int ID = article.infoBase.ID;
            article.artBase.informationBaseID = ID;
            article.article.articleBaseID = ID;
            db.ArticleBases.InsertOnSubmit(article.artBase);
            db.Articles.InsertOnSubmit(article.article);
            db.SubmitChanges();
        }

        public List<SelectListItem> GetArticleList()
        {
            var articleCategories = GetArticleCategories();
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var arty in articleCategories)
            {
                items.Add(new SelectListItem { Text = arty.name, Value = arty.ID.ToString() });
            }
            return items;
        }

        public void UpdateArticle(Article article)
        {
            Article result = GetArticleByID(article.articleBaseID);
       
            if (result != null)
            {
                result.ArticleBase.displayed = article.ArticleBase.displayed;
                result.ArticleBase.articleCategoryID = article.ArticleBase.ArticleCategory.ID;
                result.ArticleBase.InformationBase.context = article.ArticleBase.InformationBase.context;
                result.ArticleBase.InformationBase.headline = article.ArticleBase.InformationBase.headline;
            }
            db.SubmitChanges();
        }

        public void AddArticleCategory(ArticleCategory artcat)
        {
            db.ArticleCategories.InsertOnSubmit(artcat);
            db.SubmitChanges();
        }

        public void DeleteArticle(Article article)
        {
            db.Articles.DeleteOnSubmit(article);
            db.SubmitChanges();
        }

        public Article GetArticleByID(int ID)
        {
            Article result = (from a in db.Articles
                              where a.articleBaseID == ID
                              select a).SingleOrDefault();
            return result;
        }

        public IEnumerable<Article> GetArticleByCategory(ArticleCategory category)
        {
            IEnumerable<Article> result = from a in db.Articles
                                          where a.ArticleBase.articleCategoryID == category.ID
                                          && a.ArticleBase.displayed == true
                                          && a.ArticleBase.ArticleCategory.type == "Articles"
                                          select a;
            return result;
        }

        public IEnumerable<Article> GetAllArticles()
        {
            IEnumerable<Article> result = (from m in db.Articles
                                           where m.ArticleBase.displayed == true
                                           && m.ArticleBase.ArticleCategory.type == "Articles"
                                           orderby m.ArticleBase.InformationBase.date descending
                                           select m);
            return result;
        }

        public IEnumerable<ArticleCategory> GetArticleCategories()
        {
            IEnumerable<ArticleCategory> result = from ac in db.ArticleCategories
                                                  where ac.type == "Articles"
                                                  select ac;
            return result;
        }

        public ArticleCategory GetArticleCategoryByID(int ID)
        {
            ArticleCategory result = (from a in db.ArticleCategories
                                      where a.ID == ID
                                      && a.type == "Articles"
                                      select a).SingleOrDefault();
            return result;
        }

        public ArticleCategory GetArticleCategoryName(string ourName)
        {
            ArticleCategory result = (from a in db.ArticleCategories
                                      where a.name == ourName
                                      select a).SingleOrDefault();
            return result;
        }

        public IEnumerable<Article> GetArticleByCategoryID(int ID)
        {
            IEnumerable<Article> result = (from b in db.Articles
                                           where b.ArticleBase.displayed == true
                                           && b.ArticleBase.articleCategoryID == ID
                                           && b.ArticleBase.ArticleCategory.type == "Articles"
                                           orderby b.ArticleBase.InformationBase.date descending
                                           select b);
            return result;
        }

        public IEnumerable<Article> GetArticlesByCategoryIDHeadlineAlphabetical(int ID)
        {
            IEnumerable<Article> result = (from b in db.Articles
                                           where b.ArticleBase.displayed == true
                                           && b.ArticleBase.articleCategoryID == ID
                                           && b.ArticleBase.ArticleCategory.type == "Articles"
                                           orderby b.ArticleBase.InformationBase.headline ascending
                                           select b);
            return result;
        }

        public IEnumerable<Article> Get10ArticleByPageID(int ID)
        {
            IEnumerable<Article> result = (from f in db.Articles
                                           where f.ArticleBase.displayed == true
                                           orderby f.ArticleBase.InformationBase.date descending
                                           select f).Skip((ID) * 10).Take(10);
            return result;
        }

        public IEnumerable<Article> ArticlesLatestTen()
        {
            IEnumerable<Article> result = (from a in db.Articles
                                           where a.ArticleBase.displayed == true
                                           && a.ArticleBase.ArticleCategory.type == "Articles"
                                           orderby a.ArticleBase.InformationBase.date descending
                                           select a).Take(10);
            return result;
        }

        public IEnumerable<Article> GetHiddenArticles()
        {
            IEnumerable<Article> result = from a in db.Articles
                                          where a.ArticleBase.displayed != true
                                          select a;
            return result;
        }

        #endregion

        #region Question

        public void AddQuestion(QuestionContainer question)
        {
            db.InformationBases.InsertOnSubmit(question.infoBase);
            db.SubmitChanges();
            int ID = question.infoBase.ID;
            question.artBase.informationBaseID = ID;
            question.question.articleBaseID = ID;
            db.ArticleBases.InsertOnSubmit(question.artBase);
            db.Questions.InsertOnSubmit(question.question);
            db.SubmitChanges();
        }

        public void UpdateQuestion(Question question)
        {
            Question result = GetQuestionByID(question.articleBaseID);

            if (result != null)
            {
                result.ArticleBase.articleCategoryID = question.ArticleBase.ArticleCategory.ID;
                result.ArticleBase.InformationBase.headline = question.ArticleBase.InformationBase.headline;
                result.ArticleBase.InformationBase.context = question.ArticleBase.InformationBase.context;
            }
            db.SubmitChanges();
        }

        public void UpdateAnswer(Question question)
        {
            Question result = GetQuestionByID(question.articleBaseID);

            if (result != null)
            {
                result.ArticleBase.articleCategoryID = question.ArticleBase.ArticleCategory.ID;
                result.ArticleBase.Question.answer = question.ArticleBase.Question.answer;
                result.ArticleBase.displayed = question.ArticleBase.displayed;
                result.isAnswered = question.isAnswered;
                result.specialistID = question.specialistID;
            }
            db.SubmitChanges();
        }

        public void DeleteQuestion(Question question)
        {
            db.Questions.DeleteOnSubmit(question);
            db.SubmitChanges();
        }

        public Question GetQuestionByID(int ID)
        {
            Question result = (from q in db.Questions
                               where q.articleBaseID == ID
                               select q).SingleOrDefault();
            return result;
        }

        public IEnumerable<Question> GetQuestionByCategory(ArticleCategory category)
        {
            IEnumerable<Question> result = from q in db.Questions
                                           where q.ArticleBase.articleCategoryID == category.ID
                                           && q.ArticleBase.displayed == true
                                           && q.isAnswered == true
                                           select q;
            return result;
        }

        public IEnumerable<Question> GetUnansweredQuestions()
        {
            IEnumerable<Question> result = from q in db.Questions
                                           where q.ArticleBase.displayed == false
                                           || q.isAnswered == false
                                           select q;
            return result;
        }

        public List<SelectListItem> GetQuestionList()
        {
            var articleCategories = GetQuestionCategories();
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var arty in articleCategories)
            {
                items.Add(new SelectListItem { Text = arty.name, Value = arty.ID.ToString() });
            }
            return items;
        }

        public IEnumerable<Question> GetQuestionByCategoryID(int ID)
        {
            IEnumerable<Question> result = (from b in db.Questions
                                           where b.ArticleBase.displayed == true
                                           && b.isAnswered == true
                                           && b.ArticleBase.articleCategoryID == ID
                                           orderby b.ArticleBase.InformationBase.date descending
                                           select b);
            return result;
        }

        public IEnumerable<Question> GetQuestionByCategoryIDHeadlineAlphabetical(int ID)
        {
            IEnumerable<Question> result = (from b in db.Questions
                                            where b.ArticleBase.displayed == true
                                            && b.isAnswered == true
                                            && b.ArticleBase.articleCategoryID == ID
                                            orderby b.ArticleBase.InformationBase.headline ascending
                                            select b);
            return result;
        }

        public IEnumerable<Question> GetAllQuestions()
        {
            IEnumerable<Question> result = (from u in db.Questions
                                            where u.ArticleBase.displayed == true
                                            && u.isAnswered == true
                                            orderby u.ArticleBase.InformationBase.date descending
                                            select u);
            return result;
        }

        public IEnumerable<ArticleCategory> GetQuestionCategories()
        {
            IEnumerable<ArticleCategory> result = (from ac in db.ArticleCategories
                                                   where ac.type == "Questions"
                                                   select ac);
            return result;
        }

        public IEnumerable<Question> Get10QuestionsByPageID(int ID)
        {
            IEnumerable<Question> result = (from f in db.Questions
                                            where f.ArticleBase.displayed == true
                                            && f.isAnswered == true
                                            orderby f.ArticleBase.InformationBase.date descending
                                            select f).Skip((ID) * 10).Take(10);
            return result;
        }

        public IEnumerable<Question> QuestionsLatestTen()
        {
            IEnumerable<Question> result = (from q in db.Questions
                                            where q.ArticleBase.displayed == true
                                            && q.isAnswered == true
                                            && q.ArticleBase.ArticleCategory.type == "Questions"
                                            orderby q.ArticleBase.InformationBase.date descending
                                            select q).Take(10);
            return result;
        }

        #endregion

        #region PracticalInformation

        public void AddPracticalInformation(PracticalInformation pi, InformationBase inf)
        {
            db.InformationBases.InsertOnSubmit(inf);
            db.SubmitChanges();
            pi.informationBaseID = inf.ID;
            db.PracticalInformations.InsertOnSubmit(pi);
            db.SubmitChanges();
        }

        public void AddPracticalInformationCategory(InformationCategory picat)
        {
            db.InformationCategories.InsertOnSubmit(picat);
            db.SubmitChanges();
        }

        public void CreatePracticalInformation(PracticalInformation pi)
        {
            db.PracticalInformations.InsertOnSubmit(pi);
            db.SubmitChanges();
        }

        public void UpdatePracticalInformation(PracticalInformation pi)
        {
            PracticalInformation result = GetPracticalInformationByID(pi.informationBaseID);
            if (pi != null)
            {
                result.InformationBase.context = pi.InformationBase.context;
                result.InformationBase.headline = pi.InformationBase.headline;
                result.InformationBase.date = pi.InformationBase.date;
            }
            db.SubmitChanges();
        }

        public PracticalInformation GetPracticalInformationByID(int ID)
        {
            PracticalInformation result =  (from pi in db.PracticalInformations
                                           where pi.informationBaseID == ID 
                                           select pi).SingleOrDefault();
            return result;
        }

        public InformationCategory GetInformationCategoryByID(int ID)
        {
            var result = (from a in db.InformationCategories
                          where a.ID == ID
                          && a.category != "AboutUs"
                          select a).SingleOrDefault();
            return result;
        }

        public IEnumerable<InformationCategory> GetAllInformationCategories()
        {
            IEnumerable<InformationCategory> result = (from c in db.InformationCategories
                                                       where c.category != "AboutUs"
                                                       select c);
            return result;
        }

        public IEnumerable<PracticalInformation> GetPracticalInformationByCategory(InformationCategory category)
        {
            IEnumerable<PracticalInformation> result = from pi in db.PracticalInformations
                                                       where pi.informationCategoryID == category.ID
                                                       && pi.InformationCategory.category != "AboutUs"
                                                       select pi;
            return result;
        }

        #endregion

        #region AboutUs

        public void UpdateAboutUs(PracticalInformation pi)
        {
            PracticalInformation result = GetAboutUs();
            if (pi != null)
            {
                result.InformationBase.headline = pi.InformationBase.headline;
                result.InformationBase.date = pi.InformationBase.date;
                result.InformationBase.context = pi.InformationBase.context;
            }
            db.SubmitChanges();
        }

        public PracticalInformation GetAboutUs()
        {
            PracticalInformation result = (from pi in db.PracticalInformations
                                           where pi.InformationCategory.category == "AboutUs"
                                           select pi).SingleOrDefault();
            return result;
        }

        #endregion

        #region InformationBase

        public InformationBase GetInformationByID(int ID)
        {
            InformationBase result = (from a in db.InformationBases
                                      where a.ID == ID
                                      select a).SingleOrDefault();
            return result;
        }

        #endregion

        #region Survey

        public void CreateSurvey(SurveyContainer survey)
        {
            db.Surveys.InsertOnSubmit(survey.survey);
            db.SubmitChanges();
            foreach (Choice choice in survey.choices)
            {
                choice.surveyID = survey.survey.ID;
                db.Choices.InsertOnSubmit(choice);
            }
            db.SubmitChanges();
        }

        public void UpdateSurvey(SurveyContainer survey)
        {
            SurveyContainer resultSurvey = GetSurveyByID(survey.survey.ID);
            resultSurvey.survey.question = survey.survey.question;
            resultSurvey.survey.creationDate = survey.survey.creationDate;
            db.SubmitChanges();
            foreach (Choice choice in survey.choices)
            {
                Choice resultChoice = (from c in db.Choices
                                       where c.ID == choice.ID
                                       select c).SingleOrDefault();
                resultChoice.choice1 = resultChoice.choice1;
            }
            db.SubmitChanges();
        }

        public IEnumerable<Choice> UpdateChoices(int ID, User user)
        {
            var result = (from c in db.Choices
                          where c.ID == ID
                          select c).SingleOrDefault();
            result.count = (result.count + 1);
            SurveyVote vote = new SurveyVote { surveyID = result.surveyID, userID = user.ID };
            db.SurveyVotes.InsertOnSubmit(vote);
            db.SubmitChanges();
            var result2 = GetChoicesBySurveyID(GetCurrentSurvey().survey.ID);
            return result2;
        }

        public void DeleteSurvey(SurveyContainer survey)
        {
            foreach (Choice choice in survey.choices)
            {
                db.Choices.DeleteOnSubmit(choice);
            }
            db.Surveys.DeleteOnSubmit(survey.survey);
            db.SubmitChanges();
        }

        public SurveyContainer GetSurveyByID(int ID)
        {
            SurveyContainer result = new SurveyContainer();
            result.survey = (from s in db.Surveys
                             where s.ID == ID
                             select s).SingleOrDefault();
            result.choices = from c in db.Choices
                             where c.surveyID == ID
                             select c;
            return result;
        }

        public SurveyContainer GetCurrentSurvey()
        {
            SurveyContainer result = new SurveyContainer();
            result.survey = (from s in db.Surveys
                             orderby s.creationDate descending
                             select s).FirstOrDefault();
            result.choices = from c in db.Choices
                             where c.surveyID == result.survey.ID
                             select c;
            return result;
        }

        public IEnumerable<SurveyContainer> GetAllSurveys()
        {
            List<SurveyContainer> tempResult = new List<SurveyContainer>();
            IEnumerable<Survey> allSurveys = from s in db.Surveys
                                             orderby s.creationDate descending
                                             select s;
            foreach (Survey tempSurvey in allSurveys)
            {
                SurveyContainer tempContainer = new SurveyContainer();
                tempContainer.choices = from c in db.Choices
                                        where c.surveyID == tempSurvey.ID
                                        select c;
                tempContainer.survey = tempSurvey;
                tempResult.Add(tempContainer);
            }
            IEnumerable<SurveyContainer> result = tempResult;
            return result;
        }

        public IEnumerable<Choice> GetChoicesBySurveyID(int surveyID)
        {
            IEnumerable<Choice> result = from c in db.Choices
                                         where c.surveyID == surveyID
                                         select c;
            return result;
        }
        
        public bool HasUserVoted(int userID)
        {
            IEnumerable<SurveyVote> votes = from s in db.SurveyVotes
                               where s.userID == userID
                               select s;
            var survey = GetCurrentSurvey();
            foreach (var vote in votes)
            {
                if (vote.surveyID == survey.survey.ID)
                {
                    return true;
                }
            }
            return false;
        }

        #endregion

        #region Child

        public void CreateChild(Children child)
        {
            Children newchild = new Children();
            if (child != null)
            {
                newchild.name = child.name;
                newchild.profilePictureUrl = "User.png";
                newchild.publicUserID = child.publicUserID;
                newchild.description = child.description;
                newchild.age = child.age;
            }
            db.Childrens.InsertOnSubmit(newchild);
            db.SubmitChanges();
        }


        public void CreateChildWithPhoto(Picture pic, Children child)
        {
            Children newchild = new Children();
            db.Pictures.InsertOnSubmit(pic);
            db.SubmitChanges();
            string url = pic.url;
            if (child != null)
            {
                newchild.name = child.name;
                newchild.profilePictureUrl = url;
                newchild.publicUserID = child.publicUserID;
                newchild.description = child.description;
                newchild.age = child.age;
            }
            db.Childrens.InsertOnSubmit(newchild);
            db.SubmitChanges();
        }

        public void UpdateChild(Children child)
        {
            Children result = GetChildByID(child.ID);
            if (child != null)
            {
                result.name = child.name;
                result.description = child.description;
                result.age = child.age;
            }
            db.SubmitChanges();
        }

        public void UpdateChildWithPhoto(Picture pic, Children child)
        {
            Children result = GetChildByID(child.ID);

            db.Pictures.InsertOnSubmit(pic);
            db.SubmitChanges();
            string url = pic.url;
            if (child != null)
            {
                result.name = child.name;
                result.profilePictureUrl = url;
                result.description = child.description;
                result.age = child.age;
            }
            db.SubmitChanges();
        }


        public void DeleteChild(Children child)
        {
            db.Childrens.DeleteOnSubmit(child);
            db.SubmitChanges();
        }

        public Children GetChildByID(int ID)
        {
            Children result = (from c in db.Childrens
                               where c.ID == ID
                               select c).SingleOrDefault();
            return result;
        }

        public IEnumerable<Children> GetChildrenByUserID(int ID)
        {
            IEnumerable<Children> result = from c in db.Childrens
                               where c.ID == ID
                               select c;
            return result;
        }

        public void AddEntryToTimeline(TimeLineEntry entry)
        {
            db.TimeLineEntries.InsertOnSubmit(entry);
            db.SubmitChanges();
        }

        public IEnumerable<TimeLineEntry> GetChildTimeline(int childID)
        {
            IEnumerable<TimeLineEntry> result = from tle in db.TimeLineEntries
                                                where tle.childID == childID
                                                select tle;
            return result;
        }

        public IEnumerable<TimeLineEntry> GetTimeLineByID(int ID)
        {
            IEnumerable<TimeLineEntry> result = (from tle in db.TimeLineEntries
                                                 where tle.childID == ID
                                                 select tle);
            return result;

        }

        #endregion

        #region MySite

        public MySiteContainer GetMySite(int ID)
        {
            MySiteContainer result = new MySiteContainer();

            // ARTICLES:
            IEnumerable<ArticleBaseCollection> tempArt = (from a in db.ArticleBaseCollections
                                                          where a.userID == ID
                                                          select a);
            List<ArticleBase> result2 = new List<ArticleBase>();
            foreach (var res in tempArt)
            {
                ArticleBase temp2 = (from a in db.ArticleBases
                                     where a.informationBaseID == res.informationBaseID
                                     && a.displayed == true
                                     select a).SingleOrDefault();
                result2.Add(temp2);
            }
            result.myArticles = result2;

            // CONTACTS:
            IEnumerable<Contact> tempCon = (from c in db.Contacts
                                            where c.userID1 == ID || c.userID2 == ID
                                            select c);
            List<User> tempCon2 = new List<User>();
            foreach (var res in tempCon)
            {
                if (res.userID1 == ID)
                {
                    User temp = (from u in db.Users
                                 where u.ID == res.userID2
                                 select u).SingleOrDefault();
                    tempCon2.Add(temp);
                }
                else if (res.userID2 == ID)
                {
                    User temp = (from u in db.Users
                                 where u.ID == res.userID1
                                 select u).SingleOrDefault();
                    tempCon2.Add(temp);
                }

            }
            result.contacts = tempCon2;

            // CHILDREN:
            result.child = from c in db.Childrens
                           where c.publicUserID == ID
                           select c;

            result.theUser = (from c in db.Users
                             where c.ID == ID
                             select c).SingleOrDefault();

            result.pic = (from s in db.Pictures
                          where s.ID == result.theUser.profilePictureID
                          select s).SingleOrDefault();
            
            //TimeLineEntries
            List<TimeLineEntry> myEntries = new List<TimeLineEntry>();

            foreach (var child in result.child)
            {
                IEnumerable<TimeLineEntry> myEntries2 = from s in db.TimeLineEntries
                                                        where s.childID == child.ID
                                                        select s;
                foreach (var entry in myEntries2)
                {
                    myEntries.Add(entry);
                }
            }
            /*
            IEnumerable<TimeLineEntry> myEntries3 = from s in myEntries
                                                    orderby s.date descending
                                                    select s;
             * */
            result.myTimelines = myEntries.OrderByDescending(a => a.date);
            return result;          
        }

        public ArticleBaseCollection GetCollectionByUserAndID(int fakeUserID, int InformationID)
        {
            ArticleBaseCollection result = new ArticleBaseCollection();
            result = (from s in db.ArticleBaseCollections
                      where InformationID == s.informationBaseID && fakeUserID == s.userID
                      select s).SingleOrDefault();
            return result;
        }

        public void AddArticleBaseCollection(ArticleBaseCollection c)
        {
            db.ArticleBaseCollections.InsertOnSubmit(c);
            db.SubmitChanges();
        }

        public void DeleteArticleBaseCollection(ArticleBaseCollection c)
        {
            db.ArticleBaseCollections.DeleteOnSubmit(c);
            db.SubmitChanges();
        }

        public Picture GetPicByID(int ID)
        {
            User user = GetUserByID(ID);
            Picture pic = (from s in db.Pictures
                          where s.ID== user.profilePictureID
                          select s).SingleOrDefault();
             return pic;
        }

        public Picture GetPicByPictureID(int ID)
        {
            Picture pic = (from s in db.Pictures
                           where s.ID == ID
                           select s).SingleOrDefault();
            return pic;
        }

        public void UpdateMyProfile(User ourUser)
        {
            User result = GetUserByID(ourUser.ID);

            if (ourUser != null)
            {
                result.name = ourUser.name;
                result.email = ourUser.email;
                result.address = ourUser.address;
                result.age = ourUser.age;
                result.description = ourUser.description;
                result.zipCode = ourUser.zipCode;
            }

            db.SubmitChanges();
        }

        public void UpdateMyProfileAndPicture(Picture pic,User ourUser)
        {
            User result = GetUserByID(ourUser.ID);


            if (ourUser != null)
            {
                result.name = ourUser.name;
                result.email = ourUser.email;
                result.address = ourUser.address;
                result.age = ourUser.age;
                result.description = ourUser.description;
                result.zipCode = ourUser.zipCode;
            }
            db.Pictures.InsertOnSubmit(pic);
            db.SubmitChanges();
            int ID = pic.ID;
            result.profilePictureID = ID;
            db.SubmitChanges();
        }

        public Contact GetContacts(int ID1, int ID2)
        {
            IEnumerable<Contact> result = new List<Contact>();
            
                
             result = from s in db.Contacts
                      where s.userID1 == ID1 || s.userID2 == ID1
                      select s;
             
            Contact result2 = (from s in result
                               where (s.userID1 == ID1 && s.userID2 == ID2) || (s.userID1 == ID2 && s.userID2 == ID1)
                               select s).SingleOrDefault();
            return result2;
        }

        public void SaveContact(Contact toSave)
        {
            db.Contacts.InsertOnSubmit(toSave);
            db.SubmitChanges();
        }

        public void DeleteContact(int id1, int id2)
        {
            IEnumerable<Contact> result = from s in db.Contacts
                                          where s.userID1 == id1 || s.userID2 == id1
                                          select s;

            Contact result2 = (from s in result
                               where (s.userID1 == id1 && s.userID2 == id2) || (s.userID1 == id2 && s.userID2 == id1)
                              select s).SingleOrDefault();

            db.Contacts.DeleteOnSubmit(result2);
            db.SubmitChanges();

        }

        #endregion

        #region Specialists

        public void AddSpecialistCategory(SpecialistCategory specat)
        {
            db.SpecialistCategories.InsertOnSubmit(specat);
            db.SubmitChanges();
        }

        public IEnumerable<SpecialistUser> GetSpecialistsByCategoryID(int ID)
        {
            IEnumerable<SpecialistUser> result = (from s in db.SpecialistUsers
                                                  where s.specialistCategoryID == ID
                                                  select s);
            return result;
        }

        public IEnumerable<SpecialistCategory> GetSpecialistGategories()
        {
            IEnumerable<SpecialistCategory> result = from sc in db.SpecialistCategories
                                                     select sc;
            return result;
        }

        public SpecialistUser GetSpecialistByID(int ID)
        {
            SpecialistUser result = new SpecialistUser();
            result = (from s in db.SpecialistUsers
                      where s.userID == ID
                      select s).SingleOrDefault();
            return result;
        }

        public IEnumerable<SpecialistUser> GetSpecialists()
        {
            IEnumerable<SpecialistUser> result = (from s in db.SpecialistUsers
                                                  orderby s.User.name ascending
                                                  select s);
            return result;
        }
        

        #endregion

        #region Users

        public User GetUserByName(string name)
        {
            User result = (from u in db.Users
                           where u.userName == name
                           select u).SingleOrDefault();
            return result;
        }

        public User GetUserByID(int ID)
        {
            User result = (from n in db.Users
                           where n.ID == ID
                           select n).SingleOrDefault();
            return result;
        }

        public void CreateUser(User user)
        {
            db.Users.InsertOnSubmit(user);
            db.SubmitChanges();
            db.PublicUsers.InsertOnSubmit(new PublicUser { userID = user.ID });
            db.SubmitChanges();
        }

        public void AddSpecialistUser(User user, SpecialistUser spUser)
        {
            db.Users.InsertOnSubmit(user);
            db.SubmitChanges();
            spUser.userID = user.ID;
            db.SpecialistUsers.InsertOnSubmit(spUser);
            db.SubmitChanges();
        }

        #endregion

        #region Comments

        public IEnumerable<Comment> GetCommentsByID(int ID)
        {
            IEnumerable<Comment> result = from c in db.Comments
                                          where c.articleBaseID == ID
                                          orderby c.commentDate ascending
                                          select c;
            return result;
        }

        public IEnumerable<Comment> GetCommentsByUserID(int ID)
        {
            IEnumerable<Comment> result = from c in db.Comments
                                          where c.userID == ID
                                          orderby c.commentDate ascending
                                          select c;
            return result;
        }

        public void AddComment(Comment comment)
        {
            db.Comments.InsertOnSubmit(comment);
            db.SubmitChanges();
        }

        public IEnumerable<Comment> GetCommentsByArticleBaseID(int ID)
        {
            IEnumerable<Comment> result = (from c in db.Comments
                                           where c.ArticleBase.displayed == true
                                           && c.articleBaseID == ID
                                           select c);
            return result;
        }



        #endregion

        #region Myndir

        public void SavePicToDB(Picture pic)
        {
            db.Pictures.InsertOnSubmit(pic);
            db.SubmitChanges();
        }

        public void SavePicAndTimelineEntryToDB(Picture pic, TimeLineEntry ent)
        {
            db.Pictures.InsertOnSubmit(pic);
            db.SubmitChanges();
            int ID=pic.ID;
            ent.pictureID = ID;
            db.TimeLineEntries.InsertOnSubmit(ent);
            db.SubmitChanges();
        }
        public void SaveTimelineEntryToDB(TimeLineEntry ent)
        {
            db.TimeLineEntries.InsertOnSubmit(ent);
            db.SubmitChanges();
        }

        public Picture GetPictureByID(int ID)
        {
            var result = (from p in db.Pictures
                          where p.ID == ID
                          select p).SingleOrDefault();
            return result;
        }

        public IEnumerable<Picture> GetPicturesByUserID(int ID)
        {
            var result = (from p in db.Pictures
                          where p.userID == ID
                          select p);
            return result;
        }

        public bool IsPicNameValid(string path)
        {
            IEnumerable<Picture> picstemp = (from p in db.Pictures
                                             select p);
            foreach (var item in picstemp)
            {
                if (item.url == path)
                {
                    return false;
                }
            }
            return true;
        }

        #endregion

    }
}